package com.ys.otms.services.impl;

import com.ys.otms.dao.IDeductionsDao;
import com.ys.otms.dao.IDetailedTestDao;
import com.ys.otms.dto.ParamDto;
import com.ys.otms.dto.SailingScheduleDto;
import com.ys.otms.dto.TransportPlanDto;
import com.ys.otms.services.IDetailedTestServices;
import com.ys.otms.utils.DateUtil;
import com.ys.otms.utils.JsonUtil;
import com.ys.otms.utils.StringUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2018/11/8.
 */
@Service
public class DetailedTestServiceImpl implements IDetailedTestServices {
    private static Logger logger = LoggerFactory.getLogger(DetailedTestServiceImpl.class);

    @Autowired
    private IDetailedTestDao iDetailedTestDao;

    @Override
    public JSONObject selectOriginalVolumeOfGoodsRankings(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("DetailedTestServiceImpl selectOriginalVolumeOfGoodsRankings start obj{}",obj);
        String startTime = JsonUtil.getJSONObjectString(obj,"startTime");
        String endTime = JsonUtil.getJSONObjectString(obj,"endTime");
        ParamDto paramDto = new ParamDto();
        if (StringUtil.isNotEmpty(startTime)){
            paramDto.setStartTime(DateUtil.strFormatTimetamp(startTime));
        }
        if (StringUtil.isNotEmpty(endTime)){
            paramDto.setEndTime(DateUtil.strFormatTimetamp(endTime));
        }
        paramDto.setCompanyId(1);
       // paramDto.setCompanyId(Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID")));
        List<TransportPlanDto> transportPlanDtos = iDetailedTestDao.selectOriginalVolumeOfGoodsRankings(paramDto);
        JSONObject jsonObject = new JSONObject();
        if (transportPlanDtos.size()>0){
            jsonObject.put("transportPlanDtos",transportPlanDtos);
        }else{
            jsonObject.put("transportPlanDtos",null);
        }
        jsonObject.put("SUCCESS",true);
        return jsonObject;
    }

    @Override
    public JSONObject selectDestinationVolumeOfGoodsRankings(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("DetailedTestServiceImpl selectDestinationVolumeOfGoodsRankings start obj{}",obj);
        String startTime = JsonUtil.getJSONObjectString(obj,"startTime");
        String endTime = JsonUtil.getJSONObjectString(obj,"endTime");
        ParamDto paramDto = new ParamDto();
        if (StringUtil.isNotEmpty(startTime)){
            paramDto.setStartTime(DateUtil.strFormatTimetamp(startTime));
        }
        if (StringUtil.isNotEmpty(endTime)){
            paramDto.setEndTime(DateUtil.strFormatTimetamp(endTime));
        }
        paramDto.setCompanyId(1);
        // paramDto.setCompanyId(Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID")));
        List<TransportPlanDto> transportPlanDtos = iDetailedTestDao.selectDestinationVolumeOfGoodsRankings(paramDto);
        JSONObject jsonObject = new JSONObject();
        if (transportPlanDtos.size()>0){
            jsonObject.put("transportPlanDtos",transportPlanDtos);
        }else{
            jsonObject.put("transportPlanDtos",null);
        }
        jsonObject.put("SUCCESS",true);
        return jsonObject;
    }

    @Override
    public JSONObject selectLoadingAndUploadingTrendMap(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("DetailedTestServiceImpl selectLoadingAndUploadingTrendMap start obj{}",obj);
        String startTime = JsonUtil.getJSONObjectString(obj,"startTime");
        String endTime = JsonUtil.getJSONObjectString(obj,"endTime");
        ParamDto paramDto = new ParamDto();
        if (StringUtil.isNotEmpty(startTime)){
            paramDto.setStartTime(DateUtil.strFormatTimetamp(startTime));
        }
        if (StringUtil.isNotEmpty(endTime)){
            paramDto.setEndTime(DateUtil.strFormatTimetamp(endTime));
        }
        paramDto.setCompanyId(1);
        // paramDto.setCompanyId(Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID")));
        List<SailingScheduleDto> loadingDtos = iDetailedTestDao.selectLoadingTrendMap(paramDto);
        List<SailingScheduleDto> upLoadingDtos = iDetailedTestDao.selectUploadingTrendMap(paramDto);
        JSONObject jsonObject = new JSONObject();
        if (loadingDtos.size()>0){
            jsonObject.put("loadingDtos",loadingDtos);
        }else{
            jsonObject.put("loadingDtos",null);
        }
        if (upLoadingDtos.size()>0){
            jsonObject.put("upLoadingDtos",upLoadingDtos);
        }else{
            jsonObject.put("upLoadingDtos",null);
        }
        return jsonObject;
    }

    @Override
    public JSONObject selectTransportTimeRanking(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("DetailedTestServiceImpl selectTransportTimeRanking start obj{}",obj);
        String original = JsonUtil.getJSONObjectString(obj,"original");
        String destination = JsonUtil.getJSONObjectString(obj,"destination");
        ParamDto paramDto = new ParamDto();
        if (StringUtil.isNotEmpty(original)){
            paramDto.setOriginal(Integer.parseInt(original));
        }
        if (StringUtil.isNotEmpty(destination)){
            paramDto.setDestination(Integer.parseInt(destination));
        }
        paramDto.setCompanyId(1);
        // paramDto.setCompanyId(Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID")));
        List<TransportPlanDto> transportPlanDtos = iDetailedTestDao.selectTransportTimeRanking(paramDto);
        JSONObject jsonObject = new JSONObject();
        if (transportPlanDtos.size()>0){
            jsonObject.put("transportPlanDtos",transportPlanDtos);
        }else{
            jsonObject.put("transportPlanDtos",null);
        }
        return jsonObject;
    }
}
